package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ConsumerMapper;
import com.guigu.mapper.ProductCommentMapper;
import com.guigu.mapper.ProductMapper;
import com.guigu.pojo.Consumer;
import com.guigu.pojo.ProductComment;
import com.guigu.service.ProductCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductComment> implements ProductCommentService {

    @Autowired
    private ProductCommentMapper productCommentMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public List<ProductComment> getProductCommentById(Integer productId) {
        QueryWrapper<ProductComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        if (productCommentMapper.selectList(queryWrapper)!=null){
            List<ProductComment> productComments = productCommentMapper.selectList(queryWrapper);
            for (ProductComment productComment : productComments) {
                Integer consumerId = productComment.getConsumerId();
                QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
                consumerQueryWrapper.eq("consumer_id",consumerId);
                Consumer consumer = consumerMapper.selectOne(consumerQueryWrapper);
                productComment.setConsumer(consumer);
            }
            return productComments;
        }
        return null;
    }
}
